<?php
/*
 * 文件名: navigation-settings.php
 * 描述: 导航设置页面
 */

// 引入必要的WordPress函数
require_once(ABSPATH . 'wp-admin/includes/admin.php');

// 页面内容
function ningm_navigation_settings_page() {
    ?>
    <div class="wrap">
        <h1>导航设置</h1>
        <div class="container-nm navigation">
            <h2 class="text-center mb-4">导航列表</h2>
            <table class="table table-striped table-bordered" id="urlsTable">
                <thead class="thead-dark">
                    <tr>
                        <th>ID</th>
                        <th>上级</th>
                        <th>名称</th>
                        <th>连接</th>
                        <th>图标</th>
                        <th>css类</th>
                        <th>是否新窗口打开</th>
                        <th>分类</th>
                        <th>排序</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    global $wpdb;

                    // 使用try-catch块进行错误处理
                    try {
                        $results = $wpdb->get_results("SELECT * FROM ningm_urls_list ORDER BY parent, sort");

                        if ($results) {
                            $tree = buildTree($results);
                            displayTree($tree);
                        } else {
                            echo "<tr><td colspan='10' class='text-center'>没有符合条件的数据。</td></tr>";
                        }
                    } catch (Exception $e) {
                        // 捕获并处理数据库查询异常
                        echo "<tr><td colspan='10' class='text-center'>数据库查询失败: " . $e->getMessage() . "</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
            <h2 class="text-center mb-4">新增导航</h2>
            <form id="addForm">
                <input type="text" name="name" placeholder="名称" required>
                <input type="text" name="url" placeholder="URL" required>
                <input type="hidden" name="parent" value="0">
                <button type="submit" class="btn btn-success">新增</button>
            </form>
        </div>
    </div>
    <?php
}

// 构建树形结构
function buildTree($elements, $parentId = 0) {
    $branch = array();
    foreach ($elements as $element) {
        if ($element->parent == $parentId) {
            $children = buildTree($elements, $element->id);
            if ($children) {
                $element->children = $children;
            }
            $branch[] = $element;
        }
    }
    return $branch;
}

// 显示树形结构
function displayTree($tree, $level = 0) {
    foreach ($tree as $node) {
        $rowData = [
            $node->id,
            $node->parent,
            str_repeat('&nbsp;', $level * 4) . $node->name,
            $node->url,
            $node->icon,
            $node->class,
            $node->target,
            $node->type,
            $node->sort,
            '<button class="btn btn-primary btn-sm mr-2 editBtn" data-id="' . $node->id . '">编辑</button><button class="btn btn-danger btn-sm deleteBtn" data-id="' . $node->id . '">删除</button>'
        ];
        echo '<tr><td>' . join('</td><td>', $rowData) . '</td></tr>';

        if (!empty($node->children)) {
            displayTree($node->children, $level + 1);
        }
    }
}

// 添加菜单项
function ningm_navigation_settings_menu() {
    add_menu_page(
        '导航设置',
        '导航设置',
        'manage_options',
        'ningm-navigation-settings',
        'ningm_navigation_settings_page',
        'dashicons-admin-links',
        6
    );
}
add_action('admin_menu', 'ningm_navigation_settings_menu');

// AJAX处理函数
add_action('wp_ajax_ningm_navigation_action', 'ningm_navigation_ajax_handler');
function ningm_navigation_ajax_handler() {
    global $wpdb;

    $action = $_POST['action'];
    $id = isset($_POST['id']) ? intval($_POST['id']) : 0;

    switch ($action) {
        case 'add':
            $name = sanitize_text_field($_POST['name']);
            $url = esc_url_raw($_POST['url']);
            $parent = intval($_POST['parent']);
            $wpdb->insert('ningm_urls_list', array(
                'name' => $name,
                'url' => $url,
                'parent' => $parent
            ));
            break;
        case 'edit':
            // 处理编辑逻辑
            break;
        case 'delete':
            $wpdb->delete('ningm_urls_list', array('id' => $id));
            break;
    }

    wp_send_json_success();
}

